/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "NodeCpp/Data/Rel/SqlConnection.h"
#include <new>
#include "NodeCpp/Data/Rel/SqlPreparedStatement.h"
#include "NodeCpp/Data/Rel/Session.h"

namespace NodeCpp {
namespace Data {
namespace Rel {

    SqlConnection::SqlConnection(Session* _Sess)
        : Error_(false)
        , ErrCode_(0)
        , ErrString_()
        , Session_(_Sess)
        , Lock_()
        , StmtHolder_()
    {

    }

    SqlConnection::~SqlConnection(void)
    {
        _Uninitialize();
    }

    SqlPreparedStatement* SqlConnection::CreateStatement(const std::string& _Fmt)
    {
        return new (std::nothrow) SqlPlainPreparedStatement(_Fmt, *this);
    }

    SqlPreparedStatement* SqlConnection::GetStatement(std::uint32_t _Index)
    {
        if (StmtHolder_.size() <= _Index) {
            StmtHolder_.resize(_Index + 1, NULL);
        }

        SqlPreparedStatement* _Stmt = NULL;

        if (StmtHolder_[_Index] == NULL)
        {
            std::string _Fmt = Session_->GetStatementString(_Index);
            NODECPP_ASSERT(_Fmt.length());
            _Stmt = CreateStatement(_Fmt);
            if (!_Stmt->Prepare())
            {
                NODECPP_ASSERT(false && "Unable to prepare SQL statement");
                return NULL;
            }
            StmtHolder_[_Index] = _Stmt;
        }
        else {
            _Stmt = StmtHolder_[_Index];
        }

        return _Stmt;
    }

    bool SqlConnection::ExecuteStmt(int _Index, const SqlStmtParameters& _ID)
    {
        if (_Index == -1) {
            return false;
        }

        SqlPreparedStatement* _Stmt = GetStatement(_Index);
        _Stmt->Bind(_ID);
        return _Stmt->Execute();
    }

    void SqlConnection::_ReportError(void)
    {
        Session_->ErrHandler_(ErrCode_, ErrString_.c_str());
    }

    void SqlConnection::_Uninitialize(void)
    {
        /* Free statement */
        if (StmtHolder_.size() > 0) {
            for (StmtHolder::size_type x = 0;
                x < StmtHolder_.size(); ++x) {
                if (StmtHolder_[x] != NULL) {
                    delete StmtHolder_[x];
                    StmtHolder_[x] = NULL;
                }
            }
        }
    }

}}}